/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EquipamentoListaUI.java
 *
 * Created on 22/06/2011, 16:02:05
 */

package my.contacteditor;

import java.util.List;

/**
 *
 * @author Felipe
 */
public class EquipamentoListaUI extends javax.swing.JFrame {

    /** Creates new form EquipamentoListaUI */
    public EquipamentoListaUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listagem = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtnomequip = new javax.swing.JTextField();
        btnAlterar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        lblCodigo = new javax.swing.JLabel();
        mensagem = new javax.swing.JLabel();
        InserirNovo = new javax.swing.JButton();
        txtmarca = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtdefeito = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        combocarga = new javax.swing.JComboBox();
        txtnumpatrimonio = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        listagem.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        listagem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listagemMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(listagem);

        jPanel1.setBorder(new javax.swing.border.MatteBorder(null));

        jLabel3.setText("Numero de Patrimonio:");

        jLabel2.setText("Nome do Equipamento:");

        txtnomequip.setName("txtnomequip"); // NOI18N

        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        mensagem.setForeground(new java.awt.Color(255, 0, 0));

        InserirNovo.setText("Inserir novo");
        InserirNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InserirNovoActionPerformed(evt);
            }
        });

        jLabel1.setText("Marca:");

        jLabel6.setText("Carga:");

        jLabel4.setText("ITEM SELECIONADO");

        txtdefeito.setColumns(20);
        txtdefeito.setRows(5);
        jScrollPane2.setViewportView(txtdefeito);

        jLabel7.setText("Defeito:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtmarca, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(combocarga, javax.swing.GroupLayout.Alignment.TRAILING, 0, 328, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtnumpatrimonio, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtnomequip, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)))
                        .addGap(81, 81, 81)
                        .addComponent(lblCodigo)
                        .addGap(95, 95, 95)
                        .addComponent(mensagem)
                        .addGap(241, 241, 241))
                    .addComponent(jLabel4)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(btnAlterar)
                        .addGap(49, 49, 49)
                        .addComponent(btnExcluir)
                        .addGap(54, 54, 54)
                        .addComponent(InserirNovo)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtnomequip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtnumpatrimonio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtmarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                                .addComponent(lblCodigo)
                                .addGap(102, 102, 102))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel6)
                                    .addComponent(combocarga, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(jLabel7)
                                .addGap(48, 48, 48)))
                        .addGap(20, 20, 20)
                        .addComponent(mensagem)))
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAlterar)
                    .addComponent(btnExcluir)
                    .addComponent(InserirNovo)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 892, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        // TODO add your handling code here:
        Equipamento equipamento = new Equipamento();

        int vnump = Integer.parseInt(txtnumpatrimonio.getText());
        System.out.println("antes de alterar");
        boolean retorno = equipamento.Alterar(txtnomequip.getText(), vnump, txtmarca.getText(),(String)combocarga.getSelectedItem(),txtdefeito.getText() );
        if(retorno) {
            listagem.setModel(equipamento.ListarTodos());
            mensagem.setText("");
            btnAlterar.setEnabled(false);
            btnExcluir.setEnabled(false);
            limparFormulario();
        } else {
            mensagem.setText("Não foi possível alterar");
        }
}//GEN-LAST:event_btnAlterarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        // TODO add your handling code here:
        // TODO add your handling code here:
        Equipamento equipamento = new Equipamento();

        int vnumpatrimonio = Integer.parseInt(txtnumpatrimonio.getText());
        boolean retorno = equipamento.Excluir(vnumpatrimonio);
        if(retorno) {

            listagem.setModel(equipamento.ListarTodos());
            limparFormulario();
        } else {
            mensagem.setText("Não foi possível excluir");
        }
}//GEN-LAST:event_btnExcluirActionPerformed

    private void InserirNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InserirNovoActionPerformed
        // TODO add your handling code here:
        EquipamentoCadastroUI form = new EquipamentoCadastroUI();
        form.setVisible(true);
        this.dispose();
}//GEN-LAST:event_InserirNovoActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:

        Equipamento equipamento = new Equipamento();
        listagem.setModel(equipamento.ListarTodos());

/////////////// /  /  // / / //////////////////////////////////////////////////////////// //Popula os campos -- Add Combo
        Cliente cliente = new Cliente();
        List<Cliente> clientes = cliente.ListarTodosObj();

        for(Cliente item: clientes)
        {
            String texto = String.valueOf(item.getCodigo()) + " - " + item.getNome();
            combocarga.addItem(texto);
        }

    }//GEN-LAST:event_formWindowActivated
/////////////////////////////////////////////////////////////////////////////////////////////////
    private void listagemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listagemMouseClicked
int indice = listagem.getSelectedRow();
     //   String carga = (String)listagem.getValueAt(indice, 0);
        String defeito = (String)listagem.getValueAt(indice, 2);
        String nomequip = (String)listagem.getValueAt(indice, 4);
        String marca = (String)listagem.getValueAt(indice, 1);
        String numpatrimonio = (String)listagem.getValueAt(indice, 3);

        //para o alterar tem q buscar o combo selecionado
        txtdefeito.setText(defeito);
        txtmarca.setText(marca);
        txtnomequip.setText(nomequip);
        txtnumpatrimonio.setText(numpatrimonio);
        btnAlterar.setEnabled(true);
        btnExcluir.setEnabled(true);
    }//GEN-LAST:event_listagemMouseClicked
private void limparFormulario()
    {
        //lblCodigo.setText("");
        mensagem.setText("");
        txtnomequip.setText("");
        
        txtdefeito.setText("");
        txtmarca.setText("");
        txtnomequip.setText("");
        txtnumpatrimonio.setText("");
        btnExcluir.setEnabled(false);
        btnAlterar.setEnabled(false);
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EquipamentoListaUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton InserirNovo;
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JComboBox combocarga;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JTable listagem;
    private javax.swing.JLabel mensagem;
    private javax.swing.JTextArea txtdefeito;
    private javax.swing.JTextField txtmarca;
    private javax.swing.JTextField txtnomequip;
    private javax.swing.JTextField txtnumpatrimonio;
    // End of variables declaration//GEN-END:variables

}
